package com.example.coordinatorlayouttest;

/**
 * author : dut
 * e-mail : dt@xdja.com
 * date   : 2020/6/15
 * desc   :
 */
public class LinkedTest {
    public static class Node{

        String data;
        Node next;

        public Node(String data, Node next) {
            this.data = data;
            this.next = next;
        }
        public Node(String data) {
            this.data = data;
            this.next = next;
        }



        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }

    public static void main(String[] args) {
        Node head = new Node("0");
        Node node1 = new Node("1");
        Node node2 = new Node("2");
        Node node3 = new Node("3");
        head.setNext(node1);
        node1.setNext(node2);
        node2.setNext(node3);
        Node tempNode = head;
        while (tempNode != null) {
            System.out.println(tempNode.getData());
            tempNode = tempNode.getNext();
        }
        System.out.println("**************");
        Node reverseHead = reverse(head);
        while (null != reverseHead) {
            System.out.println(reverseHead.getData() + " ");
            reverseHead = reverseHead.getNext();
        }
    }

    public static Node reverse(Node head) {
        if (head == null) {
            return head;
        }
        Node pre = head;// 上一结点
        Node cur = head.getNext();// 当前结点
        Node tmp;// 临时结点，用于保存当前结点的指针域（即下一结点）
        // 当前结点为null，说明位于尾结点
        while (cur != null) {
            tmp =  cur.getNext();
            //指向前一个结点
            cur.setNext(pre);
            pre = cur;
            //移动cur为下一结点
            cur = tmp;
        }
        head.setNext(null);
        return pre;
    }


}
